package com.google.shoopinggoods.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.shoppinggoods.R;
import com.google.shoopinggoods.bean.Goods;
import com.google.shoopinggoods.global.Url;
import com.google.shoopinggoods.utils.KLog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/23.
 */
public class GoodslistAdapter extends RecyclerView.Adapter<GoodslistAdapter.GoodlistHolder> {

    private final ArrayList<Goods> list;


    public GoodslistAdapter(ArrayList<Goods> list) {
        this.list = list;
    }

    @Override
    public GoodlistHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.adapter_goodslist, null);
        return new GoodlistHolder(view);
    }

    @Override
    public void onBindViewHolder(GoodlistHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onItemClick(position);
                }
            }
        });
        Goods goods = list.get(position);
        KLog.e(KLog.Tag.QUICKEY,"goods:"+goods+", imageUrl:"+Url.ImagePrefix+",goods.getData:"+goods.getData());
        String imageUrl = Url.ImagePrefix+goods.getData().get(position).getImgs();

        Glide.with(holder.itemView.getContext()).load(imageUrl).centerCrop().placeholder(R.drawable.default_image).crossFade().into(holder.goodslistImage);
        holder.goodslistTitle.setText(goods.getData().get(position).getName());
        holder.goodslistPrice.setText("￥"+goods.getData().get(position).getOprice());

        //
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class GoodlistHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goodslist_image)
        ImageView goodslistImage;
        @BindView(R.id.goodslist_title)
        TextView goodslistTitle;
        @BindView(R.id.goodslist_price)
        TextView goodslistPrice;
        @BindView(R.id.goodslist_oprice)
        TextView goodslistOprice;
        public GoodlistHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    OnGoodsItemClickListener listener;
    public void setOnGoodsItemClickListener(OnGoodsItemClickListener listener){
        this.listener = listener;
    }

    public interface OnGoodsItemClickListener{
        void onItemClick(int position);
    }
}
